package definite.Guide.Book.JRadioButton;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class GroupRadio {

	private static final String sliceOptions[] = { "4 slices", "8 slices",
			"12 slices", "16 slices" };

	private static final String crustOptions[] = { "Sicilian", "Thin Crust",
			"Thick Crust", "Stuffed Crust" };

	public static void main(String[] args) {

		Runnable runner = new Runnable() {

			@Override
			public void run() {

				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Sample Grouping JRadioButton");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				// actionListener
				ActionListener sliceActionListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						AbstractButton abstractButton = (AbstractButton) e
								.getSource();
						System.out.println("Selected : "
								+ abstractButton.getText());
					}
				};

				ActionListener crustActionListener = new ActionListener() {

					String lastSelected;

					@Override
					public void actionPerformed(ActionEvent e) {
						AbstractButton aButton = (AbstractButton) e.getSource();
						String label = aButton.getText();
						String msgStart;
						if (label.equals(lastSelected))
							msgStart = "ReSelected  ";
						else
							msgStart = "Selected  ";
						lastSelected = label;
						System.out.println(msgStart + label);
					}
				};

				// ItemListener
				ItemListener itemListener = new ItemListener() {

					String lastSelected;

					@Override
					public void itemStateChanged(ItemEvent e) {
						AbstractButton aButton = (AbstractButton) e.getSource();
						int state = e.getStateChange();
						String label = aButton.getText();
						String msgStart = "";
						if (state == ItemEvent.SELECTED)
							msgStart = "Selected --> ";
						else if (state == ItemEvent.DESELECTED)
							msgStart = "Deselected --> ";
						System.out.println(msgStart + label);
					}
				};

				// ChangeListener
				ChangeListener changeListener = new ChangeListener() {

					@Override
					public void stateChanged(ChangeEvent e) {
						AbstractButton aButton = (AbstractButton) e.getSource();
						ButtonModel model = aButton.getModel();
						boolean armed = model.isArmed();
						boolean selected = model.isSelected();
						boolean pressed = model.isPressed();

						System.out.println("Changed : " + armed + " /"
								+ pressed + " /" + selected);
					}
				};

				// ScrollPane part
				final JTextArea textArea = new JTextArea();
				textArea.setEditable(false);
				textArea.setLineWrap(true);
				textArea.setWrapStyleWord(true);
				JScrollPane pane = new JScrollPane(textArea);
				pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

				// container part
				Container sliceContainer = JRadioButtonUtils
						.createRadioButtonGrouping(sliceOptions, "Slice Group",
								sliceActionListener);
				final Container crustContainer = JRadioButtonUtils
						.createRadioButtonGrouping(crustOptions, "Crust Group",
								crustActionListener, itemListener,
								changeListener);
				JPanel resultPane = new JPanel();
				Border border = BorderFactory
						.createTitledBorder("Order Result");
				resultPane.setBorder(border);
				resultPane.setLayout(new GridLayout(0, 1));
				resultPane.add(pane);

				// button part
				JButton btnOrder = new JButton("Order Pizza");
				btnOrder.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Enumeration<String> selectList = (Enumeration<String>) JRadioButtonUtils
								.getSelectedItem(crustContainer);
						while (selectList.hasMoreElements())
							textArea.append("Selected : "
									+ selectList.nextElement() + "\n");
					}
				});

				// add item to frame
				frame.add(sliceContainer, BorderLayout.WEST);
				frame.add(crustContainer, BorderLayout.EAST);
				frame.add(resultPane, BorderLayout.CENTER);
				frame.add(btnOrder, BorderLayout.SOUTH);
				frame.setSize(400, 300);
				frame.setResizable(false);
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
